﻿#if !UNITY_IOS || HUpdate
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System;
using System.Linq;

namespace UnityModule
{
    public partial class DownloadComponent
    {
        /// <summary>
        /// 前置载列表
        /// </summary>
        private readonly List<AssetBundleInfoV2> m_preDownloadList = new List<AssetBundleInfoV2>();

        /// <summary>
        /// 后台下载列表
        /// </summary>
        private readonly List<AssetBundleInfoV2> m_bgDownloadList = new List<AssetBundleInfoV2>();

        ////区分需要下载的文件
        private void CheckDownloadList(IEnumerator<AssetBundleInfoV2> needDownloadbundleInfos)
        {
            needDownloadbundleInfos.Reset();
            while (needDownloadbundleInfos.MoveNext())
            //检测已存在文件
            {
                var item = needDownloadbundleInfos.Current;
                if (item.pathType == 2)
                    continue;
                if (!CheckFileValid(item))
                {
                    //直接下载列表
                    m_preDownloadList.Add(item);
                }
            }
        }

        private bool CheckFileValid(AssetBundleInfoV2 asset)
        {
            string filePath = ApplicationPath.ABCachePath + asset.abNameWithHash;
            bool isExists = File.Exists(filePath);
            if (isExists)
            {
                //检查文件大小
                try
                {
                    FileInfo fileInfo = new FileInfo(filePath);
                    if (fileInfo.Length != asset.fs)
                    {
                        //断点续传不删除已有文件
                        if (DownloadClientConfig.HTTP_RESUME || asset.breakpoint == 1)
                        {
                          
                        }
                        else
                        {
                            //删除文件大小不符的文件
                            fileInfo.Delete();
                        }

                        isExists = false;
                    }
                }
                catch (Exception e)
                {
                    isExists = false;
                    Debug.LogError("CheckFiles check file length exception: " + e);
                }
            }
            return isExists;
        }



        readonly byte[] EmptyMd5 = new byte[16];
        public void VerifyFile(string filePath, byte[] md5, Action<bool> result)
        {
            if (EmptyMd5.SequenceEqual(md5))
            {
                result?.Invoke(true);
                return;
            }
            byte[] md5hash = MD5Utility.FileToMD5(filePath);
            FileInfo fi = new FileInfo(filePath);
            if (md5hash.SequenceEqual(md5))
            {
                result?.Invoke(true);
                return;
            }
            else
            {
                Debug.LogError($"{filePath}文件验证错误 {md5hash.byteToHexStr()}<>{md5.byteToHexStr()}");
                File.Delete(filePath);
                result?.Invoke(false);
                return;
            }
        }
        public void VerifyFileAsync(string filePath, byte[] md5, Action<bool> result)
        {
            if (EmptyMd5.SequenceEqual(md5))
            {
                result?.Invoke(true);
                return;
            }

            MD5Utility.FileToMD5Async(filePath, (md5hash) =>
            {
                if (md5hash.SequenceEqual(md5))
                {
                    result?.Invoke(true);
                }
                else
                {
                    Debug.LogError($"{filePath}文件验证错误 {md5hash.byteToHexStr()}<>{md5.byteToHexStr()}");
                    result?.Invoke(false);
                }

            }, (cur, total) =>
            {
                //UpdateStepUtility.SetProgress(cur, total);
            });
        }
    }
}
#endif